package com.ruoyi.ltd_web.service.impl;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ltd_web.domain.dto.LtdProductDetailItemDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ltd_web.mapper.LtdProductDetailItemMapper;
import com.ruoyi.ltd_web.domain.LtdProductDetailItem;
import com.ruoyi.ltd_web.service.ILtdProductDetailItemService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品资料明细项Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class LtdProductDetailItemServiceImpl implements ILtdProductDetailItemService
{
    @Autowired
    private LtdProductDetailItemMapper ltdProductDetailItemMapper;

    @Autowired
    private LtdContentServiceImpl ltdContentService;

    @Autowired
    private RuoYiConfig ruoYiConfig;

    /**
     * 查询产品资料明细项
     *
     * @param id 产品资料明细项主键
     * @return 产品资料明细项
     */
    @Override
    public LtdProductDetailItem selectLtdProductDetailItemById(Long id)
    {
        return ltdProductDetailItemMapper.selectLtdProductDetailItemById(id);
    }

    /**
     * 查询产品资料明细项列表
     *
     * @param ltdProductDetailItem 产品资料明细项
     * @return 产品资料明细项
     */
    @Override
    public List<LtdProductDetailItem> selectLtdProductDetailItemList(LtdProductDetailItem ltdProductDetailItem)
    {
        return ltdProductDetailItemMapper.selectLtdProductDetailItemList(ltdProductDetailItem);
    }

    /**
     * 新增产品资料明细项
     *
     * @param ltdProductDetailItem 产品资料明细项
     * @return 结果
     */
    @Override
    public int insertLtdProductDetailItem(LtdProductDetailItem ltdProductDetailItem)
    {
        ltdProductDetailItem.setCreateTime(DateUtils.getNowDate());
        return ltdProductDetailItemMapper.insertLtdProductDetailItem(ltdProductDetailItem);
    }

    /**
     * 修改产品资料明细项
     *
     * @param ltdProductDetailItem 产品资料明细项
     * @return 结果
     */
    @Override
    public int updateLtdProductDetailItem(LtdProductDetailItem ltdProductDetailItem)
    {
        ltdProductDetailItem.setUpdateTime(DateUtils.getNowDate());
        return ltdProductDetailItemMapper.updateLtdProductDetailItem(ltdProductDetailItem);
    }

    /**
     * 批量删除产品资料明细项
     *
     * @param ids 需要删除的产品资料明细项主键
     * @return 结果
     */
    @Override
    public int deleteLtdProductDetailItemByIds(Long[] ids)
    {
        return ltdProductDetailItemMapper.deleteLtdProductDetailItemByIds(ids);
    }

    /**
     * 删除产品资料明细项信息
     *
     * @param id 产品资料明细项主键
     * @return 结果
     */
    @Override
    public int deleteLtdProductDetailItemById(Long id)
    {
        return ltdProductDetailItemMapper.deleteLtdProductDetailItemById(id);
    }

    @Override
    @Transactional
    public AjaxResult addLtdProductDetailItem(LtdProductDetailItemDto ltdProductDetailItemDto) {
        try {
            LtdProductDetailItem item = new LtdProductDetailItem();
            BeanUtils.copyProperties(ltdProductDetailItemDto,item);

            if(ltdProductDetailItemDto.getType() == 0L){
                insertLtdProductDetailItem(item);
            }

            if(ltdProductDetailItemDto.getType() == 1L){
                for (MultipartFile file : ltdProductDetailItemDto.getFileList()){
                    //图片保存
                    AjaxResult result = upload_product_pic(file);
                    String url = (String) result.get("url");
                    //图片保存地址
                    item.setPictureUrl(url);
                    insertLtdProductDetailItem(item);
                }
            }
        }catch (Exception e){
            throw new ServiceException(e.toString());
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult updateDetailItem(LtdProductDetailItemDto ltdProductDetailItemDto) {
        try {
            LtdProductDetailItem item = new LtdProductDetailItem();
            BeanUtils.copyProperties(ltdProductDetailItemDto,item);

            if(ltdProductDetailItemDto.getType() == 0L){
                updateLtdProductDetailItem(item);
            }

            if(ltdProductDetailItemDto.getType() == 1L){
                for (MultipartFile file : ltdProductDetailItemDto.getFileList()){
                    //图片保存
                    AjaxResult result = upload_product_pic(file);
                    String url = (String) result.get("url");
                    //图片保存地址
                    item.setPictureUrl(url);
                    updateLtdProductDetailItem(item);
                }
            }
        }catch (Exception e){
            throw new ServiceException(e.toString());
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteLtdProductDetailItem(Long[] ids) {
        for (Long id : ids){
            ltdProductDetailItemMapper.deleteLtdProductDetailItem(id);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getItemList(Long detailId) {
        LtdProductDetailItem ltdProductDetailItem = new LtdProductDetailItem();
        ltdProductDetailItem.setDetailId(detailId);
        ltdProductDetailItem.setStatus(0L);
        return AjaxResult.success(ltdProductDetailItemMapper.selectLtdProductDetailItemList(ltdProductDetailItem));
    }

    private static final String uploadDir = "product_pic/";

    public AjaxResult upload_product_pic(MultipartFile file) {
        String uploadUrl = ruoYiConfig.getPicAddress();
        String saveUrl = ruoYiConfig.getPicProfile();
        String upload = ltdContentService.addPic(uploadUrl,saveUrl,uploadDir,file);
        AjaxResult result = new AjaxResult();
        result.put("url",upload);
        return result;
    }
}
